﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace UploadBlobToAzure
{
    public partial class AddStorageAccountToList : Form
    {
        field_validator fv = new field_validator();
        public AddStorageAccountToList()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string fileName = "uploadblobtoazure.xml";
            if (txtAccountName.Text != ""  && txtAccountKey.Text != "")
            {
                string accountName = txtAccountName.Text.Trim();
                if (!File.Exists("uploadblobtoazure.xml"))
                {
                    try
                    {
                        string str = "<xml>\n<configuration>\n" + "\t<storageaccount>" + txtAccountName.Text.Trim() + "</storageaccount>\n" + "\t<managementkeys>" + txtAccountKey.Text.Trim() + "</managementkeys>\n" + "</configuration>\n</xml>";
                        File.AppendAllText("uploadblobtoazure.xml", str);
                        MessageBox.Show("Successfully Updated uploadblobtoazure.xml");
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        Application.Exit();
                    }
                }
                else
                {
                    if (fv.testIfStorageAccountExistsInFile(accountName, fileName))
                    {
                        MessageBox.Show("An Entry for this Storage Account already exists, Please Add Some other Account Name that You might have!!");
                        txtAccountName.Text = string.Empty;
                        txtAccountKey.Text = string.Empty;
                        this.DialogResult = DialogResult.None;
                    }
                    else
                    {
                        try
                        {
                            XmlDocument xd = new XmlDocument();
                            xd.Load("uploadblobtoazure.xml");
                            XmlNode nl = xd.SelectSingleNode("//xml");
                            XmlDocument xd2 = new XmlDocument();
                            xd2.LoadXml("<configuration>\n" + "\t<storageaccount>" + txtAccountName.Text.Trim() + "</storageaccount>\n" + "\t<managementkeys>" + txtAccountKey.Text.Trim() + "</managementkeys>\n" + "</configuration>\n");
                            XmlNode n = xd.ImportNode(xd2.FirstChild, true);
                            nl.AppendChild(n);
                            xd.Save("uploadblobtoazure.xml");
                            MessageBox.Show("Successfully Updated uploadblobtoazure.xml");
                            this.Close();

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            Application.Exit();
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Enter the Proper Values for Storage Account and Storage Key.");
                this.DialogResult = DialogResult.None;
            }            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtAccountKey.Text = string.Empty;
            txtAccountName.Text = string.Empty;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
